package com.kfm.hotel.service.impl;

import com.anji.captcha.util.AESUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kfm.hotel.domain.SysUser;
import com.kfm.hotel.service.SysUserService;
import com.kfm.hotel.mapper.SysUserMapper;
import com.kfm.hotel.util.MD5Util;
import org.apache.poi.ss.formula.functions.Count;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
* @author Lenovo
* @description 针对表【sys_user】的数据库操作Service实现
* @createDate 2023-06-01 22:18:27
*/
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService {



    //用户功能实现
    @Override
    public SysUser login(SysUser user){
        //前端密码解密后传输到数据库时进行加密
        user.setPassword(MD5Util.MD5(user.getPassword(), user.getUsername()));
        //从数据库中比对加密后的密码
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.select("id", "username", "email", "name", "phone", "create_time", "update_time", "state");

        wrapper.eq("username", user.getUsername());
        wrapper.eq("password", user.getPassword());
        wrapper.eq("state", 1);
        SysUser one = getOne(wrapper);
        return one;
    }

    //记住我功能实现
    @Override
    public SysUser login(String username, String password){
        //实例化对象
        SysUser user = new SysUser();
        //登录页面输入的密码和用户名
        user.setUsername(username);
        user.setPassword(password);
        //返回登录页面
        return login(user);

    }
//实现分页功能
    @Override
    public PageInfo<SysUser> list(SysUser user, Integer page, Integer limit) {
        //分页
        PageHelper.startPage(page, limit);
        //查询条件
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "username", "email","name", "phone", "create_time", "update_time", "state");
       //
        if(user.getUsername()!=null&&!user.getUsername().equals("")){
            queryWrapper.like("username",user.getUsername());
        }
        //分页中查询状态为1，删除状态为0
        queryWrapper.eq("state",1);
        queryWrapper.eq("is_del",0);

        //查询并封装成pageinfo对象
        //getBaseMapper():取持久层
        List<SysUser> sysUsers= getBaseMapper().listSelector(user);
        PageInfo<SysUser> pageInfo= new PageInfo<>(sysUsers);
        return pageInfo;
    }
//添加用户功能:
    @Override
    public int saveSysUser(SysUser user) {
        //1.添加用户名
       saveUserName(user);

        //2.添加密码
        //AES解密
        String pass="";
        try {
            pass=AESUtil.aesDecrypt(user.getPassword(),user.getKey());
        } catch (Exception e) {
            //抛异常，在MyException类中接收异常
            throw new RuntimeException(e);
        }
        //MD5加密
        user.setPassword(MD5Util.MD5(pass,user.getUsername()));
        return save(user) ? 1:-1;
    }

    @Override
    public void existUsername(SysUser sysUser) {
        //判断用户名是否存在
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.select("count(1) as num");
        wrapper.eq("username", sysUser.getUsername());
        wrapper.eq("is_del",0);
        wrapper.eq("state",1);
        Map<String, Long> map = getMap(wrapper);
        if (map.get("num") > 0){
            throw new RuntimeException("用户名已存在");
        }

    }

    //添加用户名：查询数据库中是否有相同的用户名
    public void saveUserName(SysUser user){
        //判断用户名是否存在,查询count即可
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.select("count(1) as num");
        //姓名是否相等
        wrapper.eq("username",user.getUsername());
        //是否被前端删除
        wrapper.eq("is_del",0);
        //是否禁用状态
        wrapper.eq("state",1);

        Map<String,Long> map = getMap(wrapper);
        if(map.get("num")>0){
           throw new RuntimeException("用户名已经存在");
        }

    }
}




